import VueRouter from "vue-router";
import Storage from "@/store/storage";
import Store from "@/store";
import {Message} from "element-ui";

const routes = [
    {
        path: '/',
        name: '',
        redirect: {
            path: '/hotel/room'
        }
    },
    {
        name: 'hotel',
        path: '/hotel',
        component: () => import('@/view/hotel/Hotel.vue'),
        children: [{
            name: 'hotel-room',
            path: 'room',
            component: () => import('@/view/hotel/Room.vue')
        },{
            name: 'hotel-order',
            path: 'order',
            component: () => import('@/view/hotel/Order.vue'),
            meta: {
                requireAuth: true,
            }
        },{
            name: 'hotel-oneself',
            path: 'oneself',
            component: () => import('@/view/hotel/Oneself.vue'),
            meta: {
                requireAuth: true,
            }
        },{
            name: 'hotel-index',
            path: 'index',
            component: () => import('@/view/hotel/Index.vue')
        }]
    },
    {
        name: 'test',
        path: '/test',
        component: () => import('@/view/Test.vue'),
        meta: {
            requireAuth: true,
        }
    },
    {
        name: 'order',
        path: '/order',
        component: () => import('@/view/hotel/Order.vue'),
        meta: {
            requireAuth: true,
        }
    },
    {
        name: 'admin',
        path: '/admin',
        component: () => import('@/view/admin/Admin.vue'),
        children: [{
            name: 'login',
            path: '',
            component: () => import('@/view/admin/Login.vue')
        },{
            path: 'manage',
            component: () => import('@/view/admin/Manage.vue'),
            children:[{
                path: 'room',
                name: 'manage-room',
                component: () => import('@/view/admin/Room.vue')
            },{
                path: 'checkin',
                name: 'manage-checkin',
                component: () => import('@/view/admin/CheckInOut.vue')
            },{
                path: 'hotel',
                name: 'manage-hotel',
                component: () => import('@/view/admin/Hotel.vue')
            },{
                path: 'oneself',
                name: 'manage-oneself',
                component: () => import('@/view/admin/Oneself.vue')
            }]
        }]
    }
]

const router = new VueRouter({
    mode: 'hash',
    routes: routes,
    base: process.env.BASE_URL
})

if (Storage.get(Store.state.token.tokenName)) {
    Store.commit("setTokenValue", Storage.get(Store.state.token.tokenName));
}
router.beforeEach((to, from, next) => {
    // 判断要去的路由有没有requiresAuth
    // to.matched.some(r => r.meta.requireAuth) or to.meta.requiresAuth
    if (to.matched.some(r => r.meta.requireAuth)) {
        console.log(Store.getters.getTokenValue)
        console.log("a")
        if (Store.getters.getTokenValue) {
            next(); //有token,进行request请求，后台还会验证token
        } else {
            Message.error({
                message: "请登录",
                duration: 5 * 1000
            })
            next({
                path: "/hotel/room",
                // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由，这要进一步在登陆页面判断
                // query: { redirect: to.fullPath }
            });
        }
    } else {
        next(); //如果无需token,那么随它去吧
    }
})
export default router